home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / st_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  10.9 KB  |  475 lines

  1. /* st_read.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: st_read.c,v 4.0 1994/01/11 17:56:51 espie Exp espie $
  6.  * $Log: st_read.c,v $
  7.  * Revision 4.0  1994/01/11  17:56:51  espie
  8.  * Use new open.
  9.  *
  10.  * Revision 1.5  1994/01/09  17:36:22  Espie
  11.  * Generalized open.c.
  12.  *
  13.  * Revision 1.4  1994/01/05  16:10:49  Espie
  14.  * *** empty log message ***
  15.  *
  16.  * Revision 1.3  1994/01/05  14:54:09  Espie
  17.  * *** empty log message ***
  18.  *
  19.  * Revision 1.2  1994/01/05  13:50:43  Espie
  20.  * Cosmetic change.
  21.  *
  22.  * Revision 1.1  1993/12/26  00:55:53  Espie
  23.  * Initial revision
  24.  *
  25.  * Revision 3.13  1993/12/04  16:12:50  espie
  26.  * BOOL -> boolean.
  27.  *
  28.  * Revision 3.12  1993/12/02  15:45:33  espie
  29.  * Added lots of checks for malloc, plus count of bytes read.
  30.  *
  31.  * Revision 3.11  1993/11/27  17:29:53  espie
  32.  * Fixed up malloc problems.
  33.  *
  34.  * Revision 3.10  1993/11/17  15:31:16  espie
  35.  * *** empty log message ***
  36.  *
  37.  * Revision 3.9  1993/11/11  20:00:03  espie
  38.  * Amiga support.
  39.  *
  40.  * Revision 3.7  1993/07/17  12:00:30  espie
  41.  * Added other commands (numerous).
  42.  *
  43.  * Revision 3.2  1992/11/22  17:20:01  espie
  44.  * Checks for finetune ?
  45.  *
  46.  * Revision 3.1  1992/11/19  20:44:47  espie
  47.  * Protracker commands.
  48.  *
  49.  * Revision 3.0  1992/11/18  16:08:05  espie
  50.  * New release.
  51.  *
  52.  * Revision 2.16  1992/11/17  17:06:25  espie
  53.  * fix_xxx for better speed.
  54.  * Added some sample information in the dump.
  55.  * Added transpose feature.
  56.  * Feature fix: length 1 sample should be empty.
  57.  * Corrected repeat length problems concerning badly formed files,
  58.  * added signature checking for new tracker files.
  59.  * Corrected small problem with repeat being too short.
  60.  * Coded error types. More amiga specific stuff.
  61.  *
  62.  * Revision 1.17  1991/11/17  16:30:48  espie
  63.  * Rationnalized error recovery.
  64.  * There was a bug: you could try to deallocate
  65.  * stuff in no-noland. Also, strings never got
  66.  * to be freed.
  67.  * Centralized error control to error_song.
  68.  * Added a new test on length, aborts most modules now.
  69.  * Maybe should say it as well.
  70.  * Added checkpoints for early return if file too short.
  71.  * Added memory recovery and error control.
  72.  * Suppressed ! warning for bad note.
  73.  * Added note support.
  74.  * Corrected length and rep_length/rep_offset
  75.  * which are given in words and should be converted to
  76.  * bytes.
  77.  */
  78.  
  79. #include "defs.h"
  80.  
  81. #ifdef MALLOC_NOT_IN_STDLIB
  82. #include <malloc.h>
  83. #else
  84. #include <stdlib.h>
  85. #endif
  86. #include <stdio.h>
  87. #include <string.h>
  88. #include <ctype.h>
  89. #include <assert.h>
  90.  
  91. #include "extern.h"
  92. #include "song.h"
  93. #include "channel.h"
  94.  
  95.  
  96. ID("$Id: st_read.c,v 4.0 1994/01/11 17:56:51 espie Exp espie $")
  97.  
  98. /***
  99.  *
  100.  * Low level st-file access routines 
  101.  *
  102.  ***/
  103.  
  104. #define MAX_LEN 50
  105. /* s = getstring(f, len):
  106.  * gets a soundtracker string from file f.
  107.  * I.e, it is a fixed length string terminated
  108.  * by a 0 if too short. Length should be
  109.  * smaller than MAX_LEN.
  110.  */
  111. LOCAL char *getstring(f, len)
  112. struct exfile *f;
  113. int len;
  114.    {
  115.    static char s[MAX_LEN];
  116.    char *new;
  117.    int i;
  118.         
  119.    assert(len < MAX_LEN);
  120.    for (i = 0; i < len; i++)
  121.       s[MIN(i, MAX_LEN - 1)] = getc_file(f);
  122.    s[MIN(len, MAX_LEN - 1)] = '\0';
  123.    new = malloc(strlen(s)+1);
  124.    if (!new) 
  125.       return NULL;
  126.  
  127.    return strcpy(new, s);
  128.    }
  129.  
  130. /* byteskip(f, len)
  131.  * same as fseek, xcpt it works on stdin
  132.  */
  133. LOCAL void byteskip(f, len)
  134. struct exfile *f;
  135. int len;
  136.    {
  137.    int i;
  138.  
  139.    for (i = 0; i < len; i++)
  140.       getc_file(f);
  141.    }
  142.  
  143. /* v = getushort(f)
  144.  * reads an unsigned short from f
  145.  */
  146. LOCAL int getushort(f)
  147. struct exfile *f;
  148.    {
  149.    int i;
  150.  
  151.       /* order dependent !!! */
  152.    i = getc_file(f) << 8;
  153.    return i | getc_file(f);
  154.    }
  155.  
  156.  
  157. /* fill_sample_info(info, f):
  158.  * fill sample info with the information at current position of
  159.  * file f. Allocate memory for storing the sample, also.
  160.  * fill_sample_info is guaranteed to give you an accurate snapshot
  161.  * of what sample should be like. In particular, length, rp_length,
  162.  * start, rp_start, fix_length, fix_rp_length will have the values
  163.  * you can expect if part of the sample is missing.
  164.  */
  165. LOCAL void fill_sample_info(info, f)
  166. struct sample_info *info;
  167. struct exfile *f;
  168.    {
  169.    info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  170.    if (!info->name)
  171.       {
  172.       error = OUT_OF_MEM;
  173.       return;
  174.       }
  175.    info->length = getushort(f);
  176.    info->finetune = getc_file(f);
  177.    if (info->finetune > 15)
  178.       info->finetune = 0;
  179.    info->volume = getc_file(f);
  180.    info->volume = MIN(info->volume, MAX_VOLUME);
  181.    info->rp_offset = getushort(f);
  182.    info->rp_length = getushort(f);
  183.  
  184.    /* the next check is for old modules for which
  185.     * the sample data types are a bit confused, so
  186.     * that what we were expecting to be #words is #bytes.
  187.     */
  188.       /* not sure I understand the -1 myself, though it's
  189.        * necessary to play kawai-k1 correctly 
  190.        */
  191.    if (info->rp_length + info->rp_offset - 1 > info->length)
  192.       info->rp_offset /= 2;
  193.     
  194.    if (info->rp_length + info->rp_offset > info->length)
  195.       info->rp_length = info->length - info->rp_offset;
  196.  
  197.    info->length *= 2;
  198.    info->rp_offset *= 2;
  199.    info->rp_length *= 2;
  200.       /* in all logic, a 2-sized sample could exist,
  201.        * but this is not the case, and even so, some
  202.        * trackers output empty instruments as being 2-sized.
  203.        */
  204.    if (info->length <= 2)
  205.       {
  206.       info->start = NULL;
  207.       info->length = 0;
  208.       }
  209.    else
  210.       {
  211.       info->start = (SAMPLE *)alloc_sample(info->length);
  212.       if (!info->start)
  213.          {
  214.          error = OUT_OF_MEM;
  215.          return;
  216.          }
  217.  
  218.       if (info->rp_length > 2)
  219.          info->rp_start = info->start + info->rp_offset;
  220.       else
  221.          {
  222.          info->rp_start = NULL;
  223.          info->rp_length = 0;
  224.          }
  225.       }
  226.  
  227.    if (info->length > MAX_SAMPLE_LENGTH)
  228.       error = CORRUPT_FILE;
  229.    info->fix_length = int_to_fix(info->length);
  230.    info->fix_rp_length = int_to_fix(info->rp_length);
  231.    }
  232.  
  233. LOCAL void fill_song_info(info, f)
  234. struct song_info *info;
  235. struct exfile *f;
  236.    {
  237.    int i;
  238.    int p;
  239.  
  240.    info->length = getc_file(f);
  241.    getc_file(f);
  242.    info->maxpat = -1;
  243.    for (i = 0; i < NUMBER_PATTERNS; i++)
  244.       {
  245.       p = getc_file(f);
  246.       if (p >= NUMBER_PATTERNS)
  247.          p = 0;
  248.       if (p > info->maxpat)
  249.          info->maxpat = p;
  250.       info->patnumber[i] = p;
  251.       }
  252.    info->maxpat++;
  253.    if (info->maxpat == 0 || info->length == 0)
  254.       error = CORRUPT_FILE;
  255.    }
  256.  
  257. LOCAL void fill_event(e, f)
  258. struct event *e;
  259. struct exfile *f;
  260.    {
  261.    int a, b, c, d;
  262.  
  263.    a = getc_file(f);
  264.    b = getc_file(f);
  265.    c = getc_file(f);
  266.    d = getc_file(f);
  267.    e->sample_number = (a & 0x10) | (c >> 4);
  268.    e->effect = c & 0xf;
  269.    e->parameters = d;
  270.    if (e->effect == EFF_EXTENDED)
  271.       {
  272.       e->effect = EXT_BASE + HI(e->parameters);
  273.       e->parameters = LOW(e->parameters);
  274.       }
  275.    if (e->effect == 0)
  276.       e->effect = e->parameters ? EFF_ARPEGGIO : EFF_NONE;
  277.    if (e->effect == EFF_SKIP)
  278.       e->parameters = HI(e->parameters) * 10 + LOW(e->parameters);
  279.    e->pitch = ( (a & 15) << 8 ) | b;
  280.    e->note = find_note(e->pitch);
  281.    }
  282.  
  283. LOCAL void fill_pattern(pattern, f)
  284. struct block *pattern;
  285. struct exfile *f;
  286.    {
  287.    int i, j;
  288.  
  289.    for (i = 0; i < BLOCK_LENGTH; i++)
  290.       for (j = 0; j < NUMBER_TRACKS; j++)
  291.          fill_event(&(pattern->e[j][i]), f);
  292.    }
  293.  
  294.  
  295. LOCAL void read_sample(info, f)
  296. struct sample_info *info;
  297. struct exfile *f;
  298.    {
  299.    if (info->start)
  300.       obtain_sample(info->start, info->length, file_handle(f));
  301.    }
  302.  
  303.  
  304.  
  305.  
  306. /***
  307.  *
  308.  *  new_song: allocates a new structure for a song.
  309.  *  clears each and every field as appropriate.
  310.  *
  311.  ***/
  312. LOCAL struct song *new_song()
  313.    {
  314.    struct song *new;
  315.    int i;
  316.  
  317.    new = (struct song *)malloc(sizeof(struct song));
  318.    if (!new) 
  319.       {
  320.       error = OUT_OF_MEM;
  321.       return NULL;
  322.       }
  323.    new->title = NULL;
  324.    new->info.length = 0;
  325.    new->info.maxpat = -1;
  326.    new->info.transpose = 0;
  327.    new->info.pblocks = NULL;
  328.    for (i = 0; i < NUMBER_SAMPLES; i++)
  329.       {
  330.       new->samples[i].finetune = 0;
  331.       new->samples[i].name = NULL;
  332.       new->samples[i].length = 0;
  333.       new->samples[i].start = NULL;
  334.       new->samples[i].rp_start = NULL;
  335.       new->samples[i].fix_length = 0;
  336.       new->samples[i].fix_rp_length = 0;
  337.       }
  338.    return new;
  339.    }
  340.  
  341. /* release_song(song): gives back all memory 
  342.  * occupied by song. Assume that each structure
  343.  * has been correctly allocated by a call to the
  344.  * corresponding new_XXX function.
  345.  */
  346. void release_song(song)
  347. struct song *song;
  348.    {
  349.    int i;
  350.  
  351.    if (!song)
  352.       return;
  353.    for (i = 0; i < NUMBER_SAMPLES; i++)
  354.       {
  355.       if (song->samples[i].start)
  356.          free_sample(song->samples[i].start);
  357.       if (song->samples[i].name)
  358.          free(song->samples[i].name);
  359.       }
  360.    if (song->info.pblocks)
  361.       free(song->info.pblocks);
  362.    if (song->title)
  363.       free(song->title);
  364.    free(song);
  365.    }
  366.  
  367. /* error_song(song): what we should return
  368.  * if there was an error. Actually, is mostly
  369.  * useful for its side effects.
  370.  */
  371. LOCAL struct song *error_song(song)
  372. struct song *song;
  373.    {
  374.    release_song(song);
  375.    return NULL;
  376.    }
  377.  
  378. /* bad_sig(f): read the signature on file f
  379.  * and returns TRUE if it is not a known sig.
  380.  */
  381. LOCAL boolean bad_sig(f)
  382. struct exfile *f;
  383.    {
  384.    char a, b, c, d;
  385.  
  386.    a = getc_file(f);
  387.    b = getc_file(f);
  388.    c = getc_file(f);
  389.    d = getc_file(f);
  390.    if (a == 'M' && b == '.' && c == 'K' && d == '.')
  391.       return FALSE;
  392.    if (a == 'M' && b == '&' && c == 'K' && d == '!')
  393.       return FALSE;
  394.    if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  395.       return FALSE;
  396.    return TRUE;
  397.    }
  398.  
  399. /* s = read_song(f, type): tries to read a song s
  400.  * of type NEW/OLD in file f. Might fail, i.e.,
  401.  * returns NULL if file is not a mod file of the
  402.  * correct type.
  403.  */
  404. struct song *read_song(f, type)
  405. struct exfile *f;
  406. int type;
  407.    {
  408.    struct song *song;
  409.    int i;
  410.    int ninstr;
  411.  
  412.    error = NONE;
  413.  
  414.    if (type == NEW || type == NEW_NO_CHECK)
  415.       ninstr = 31;
  416.    else
  417.       ninstr = 15;
  418.  
  419.    song = new_song();
  420.    if (!song)
  421.       return error_song(song);
  422.    song->title = getstring(f, TITLE_MAXLENGTH);
  423.    if (error != NONE)
  424.       return error_song(song);
  425.  
  426.    for (i = 1; i <= ninstr; i++)
  427.       {
  428.       fill_sample_info(&song->samples[i], f);
  429.       if (error != NONE)
  430.          return error_song(song);
  431.       }
  432.  
  433.    fill_song_info(&song->info, f);
  434.  
  435.    if (error != NONE)
  436.       return error_song(song);
  437.  
  438.    if (type == NEW && bad_sig(f))
  439.       return error_song(song);
  440.  
  441.    if (type == NEW_NO_CHECK)
  442.       byteskip(f, 4);
  443.         
  444.  
  445.    song->info.pblocks = (struct block *)
  446.       malloc(sizeof(struct block) * song->info.maxpat);
  447.    if (!song->info.pblocks)
  448.       {
  449.       error = OUT_OF_MEM;
  450.       return error_song(song);
  451.       }
  452.    for (i = 0; i < song->info.maxpat; i++)
  453.       {
  454.       fill_pattern(song->info.pblocks + i, f);
  455.       if (error != NONE)
  456.          return error_song(song);
  457.       }
  458.          /* future code... */
  459.    song->samples_start = tell_file(f);
  460.  
  461. #if 0
  462.    if (feof(f))
  463.       for (i = 1; i <= ninstr; i++)
  464.          find_sample(&song->samples[i]);
  465.    else
  466. #endif
  467.       for (i = 1; i <= ninstr; i++)
  468.          read_sample(&song->samples[i], f);
  469.     
  470.    if (error != NONE)
  471.       return error_song(song);
  472.    return song;
  473.    }
  474.  
  475.